﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;
using MLM.AdminCP.Class;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class ChangeMyPassword : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SaveChangePassBtn.Click += new EventHandler(SaveChangePassBtn_Click);
        }

        void SaveChangePassBtn_Click(object sender, EventArgs e)
        {
            MLMDataContext ct = new MLMDataContext();
            long userId = Permission.GetCurrentUserId();
            var pass = CurrentPassTxt.Text.Trim();
            if (ct.Users.FirstOrDefault(it => it.UserId == userId && it.Password == pass) == null)
            {
                Message.ShowError("Current password not match!");
            }
            else
            {
                ct.Users.FirstOrDefault(it => it.UserId == userId && it.Password == pass).Password =
                    NewPassTxt.Text.Trim();
                ct.SaveChanges();
                //try
                //{
                //    Email.Send_Email(ct.UserInfos.FirstOrDefault(it => it.UserID == userId).Email, "Password changed",
                //                     CommonSettingHelper.GetCommonSetting(ct,
                //                         CommonSettingsContstants.EMAIL_SENDNEWPASSWORDTEMPLATE).Replace("{{USER_PASSWORD}}", pass));
                //}
                //catch (Exception)
                //{

                //}
                Message.ShowSuccess("Password changed!");
            }
        }
    }
}